
package edu.moravian.CollisionPhysics;

import edu.moravian.entity.BallEntity;
import edu.moravian.math.Point2D;
import edu.moravian.math.Vector2D;
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author ronaghan
 */
public class BallLoader
{
    
    //The keys we're going to be looking for 
    private static final String DELTA = "delta";
    private static final String ELASTICITY = "elasticity";
    
    private static final String BALL_ONE_X_LOC = "ballOneXLoc"; 
    private static final String BALL_ONE_Y_LOC = "ballOneYLoc"; 
    private static final String BALL_ONE_X_HEAD = "ballOneXHead"; 
    private static final String BALL_ONE_Y_HEAD = "ballOneYHead"; 
    private static final String BALL_ONE_SPEED = "ballOneSpeed"; 
    private static final String BALL_ONE_RADIUS = "ballOneRadius"; 
    private static final String BALL_ONE_MASS = "ballOneMass"; 

    private static final String BALL_TWO_X_LOC = "ballTwoXLoc"; 
    private static final String BALL_TWO_Y_LOC = "ballTwoYLoc"; 
    private static final String BALL_TWO_X_HEAD = "ballTwoXHead"; 
    private static final String BALL_TWO_Y_HEAD = "ballTwoYHead"; 
    private static final String BALL_TWO_SPEED = "ballTwoSpeed"; 
    private static final String BALL_TWO_RADIUS = "ballTwoRadius"; 
    private static final String BALL_TWO_MASS = "ballTwoMass"; 
    
    
    //the properties we're trying to load 
    private BallEntity ballOne;
    private BallEntity ballTwo;
    private double delta;
    private double elasticity;
    
    private File file;
    
    /**
     * Load all the properties for two balls whose information is in the specified file 
     * 
     * @param file the file to load the ball properties in from 
     * 
     * @throws FileNotFoundException if the specified file was not found 
     * @throws IOException if a more serious io exception occurred while trying to read
     */
    public BallLoader(File file) throws FileNotFoundException, IOException
    {
        //store the file so we can extract it for testing
        this.file = file;
        
        //Read in the properties for the balls 
        Properties ballProps = new Properties();
        ballProps.load(new FileInputStream(file));
        
        
        //load the elasticity and the delta
        delta = getDoubleFromProp(ballProps, DELTA);
        elasticity = getDoubleFromProp(ballProps, ELASTICITY);
        
        
        //load ball one 
        double oneXLoc = getDoubleFromProp(ballProps, BALL_ONE_X_LOC);
        double oneYLoc = getDoubleFromProp(ballProps, BALL_ONE_Y_LOC);
        
        double oneXHead = getDoubleFromProp(ballProps, BALL_ONE_X_HEAD);
        double oneYHead = getDoubleFromProp(ballProps, BALL_ONE_Y_HEAD);
        
        double oneSpeed = getDoubleFromProp(ballProps, BALL_ONE_SPEED); 
        double oneRadius = getDoubleFromProp(ballProps, BALL_ONE_RADIUS); 
        double oneMass = getDoubleFromProp(ballProps, BALL_ONE_MASS);
        
        Point2D oneLoc = new Point2D(oneXLoc, oneYLoc);
        
        Vector2D oneHead = new Vector2D(oneXHead, oneYHead);
        
        ballOne = new BallEntity(oneLoc, oneHead, oneSpeed, oneRadius, oneMass);
        
            
        //load ball two 
        double twoXLoc = getDoubleFromProp(ballProps, BALL_TWO_X_LOC);
        double twoYLoc = getDoubleFromProp(ballProps, BALL_TWO_Y_LOC);
        
        double twoXHead = getDoubleFromProp(ballProps, BALL_TWO_X_HEAD);
        double twoYHead = getDoubleFromProp(ballProps, BALL_TWO_Y_HEAD);
        
        double twoSpeed = getDoubleFromProp(ballProps, BALL_TWO_SPEED); 
        double twoRadius = getDoubleFromProp(ballProps, BALL_TWO_RADIUS); 
        double twoMass = getDoubleFromProp(ballProps, BALL_TWO_MASS);
        
        Point2D twoLoc = new Point2D(twoXLoc, twoYLoc);
        
        Vector2D twoHead = new Vector2D(twoXHead, twoYHead);
        
        ballTwo = new BallEntity(twoLoc, twoHead, twoSpeed, twoRadius, twoMass);
                
    }
    
    
    /**
     * Helper to get a double value from a specified property key
     * 
     * @param prop the property to look in 
     * @param propKey the key to look for 
     * 
     * @return double value of value associated with key in specified property
     */
    private double getDoubleFromProp(Properties prop, String propKey)
    {
        String propValue = prop.getProperty(propKey);
        
        double ret = Double.parseDouble(propValue);
        
        return ret;
    }
    
    
    /**
     * Get the first ball
     * 
     * @return ball one
     */
    public BallEntity getBallOne()
    {
        return ballOne;
    }
    
    
    /**
     * Get the second ball
     * @return 
     */
    public BallEntity getBallTwo()
    {
        return ballTwo;
    }
    
    
    /**
     * Get the elasticity between the two balls 
     * 
     * @return the elasticity
     */
    public double getElasticity()
    {
        return elasticity;
    }
    
    
    /**
     * Get the delta
     * 
     * @return get the time frame we care about between these two balls
     */
    public double getDelta()
    {
        return delta;
    }
    
    /**
     * Return the path for the file containing the balls
     * 
     * @return 
     */
    public File getFilePath()
    {
        return file;
    }
    
    
    
    
    

}



